package com.zx.idc.backend.gui.common.base;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.common.spring.SpringBeanUtil;
import com.zx.idc.ds.common.constant.DataFeatureConstant;
import com.zx.idc.ds.common.constant.DelFlagConstant;
import com.zx.idc.ds.common.constant.ReportStatusConstant;
import com.zx.idc.ds.common.entity.BaseEntity;
import com.zx.idc.ds.common.service.BaseService;
import org.joor.Reflect;
import org.quartz.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author rxliuli
 */
abstract class BaseReportJob<Entity extends BaseEntity, Service extends BaseService<Entity>, ReportService extends BaseReportService<?>> implements Job, BaseDependencyInjectionApi {
    private static final Logger LOG = LoggerFactory.getLogger(BaseReportJob.class);

    protected final int dateFeature;
    protected final String idName;
    protected final String jobName;
    protected final Class<Entity> entityClass;

    protected final Service service;
    protected final ReportService reportService;

    @SuppressWarnings("unchecked")
    protected BaseReportJob(int dateFeature, String idName, String jobName) {
        this.dateFeature = dateFeature;
        this.idName = idName;
        this.jobName = jobName;
        //从父类第一个参数获取实体类，第二个参数获取实体Service，第三个参数获取实体ReportService
        Type superclass = this.getClass().getGenericSuperclass();
        ParameterizedType parameterized = (ParameterizedType) superclass;
        Type[] types = parameterized.getActualTypeArguments();
        this.entityClass = (Class<Entity>) types[0];
        final Class<Service> serviceClass = (Class<Service>) types[1];
        final Class<ReportService> reportServiceClass = (Class<ReportService>) types[2];
        this.service = SpringBeanUtil.getBean(serviceClass);
        this.reportService = SpringBeanUtil.getBean(reportServiceClass);
    }

    protected BaseReportJob(int dateFeature, String jobName) {
        this(dateFeature, "id", jobName);
    }


    protected void exec() {
        final List<Entity> list = dateFeature != DataFeatureConstant.DELETE
            ? service.selectList(
            new EntityWrapper<Entity>(
                Reflect.on(entityClass).create()
                    .set("reportStatus", ReportStatusConstant.NOT_REPORTED)
                    .set("dataFeature", dateFeature)
                    .get()
            ).setSqlSelect(idName))
            : service.selectListForDelReport(
            Reflect.on(entityClass).create()
                .set("reportStatus", ReportStatusConstant.NOT_REPORTED)
                .set("dataFeature", dateFeature)
                .set("delFlag", DelFlagConstant.DELETE)
                .get());
        LOG.info("{}任务执行开始。即将上报的记录条数：{}", jobName, list.size());
        if (list.size() > 0) {
            if (dateFeature == DataFeatureConstant.NEW) {
                list.forEach(en -> reportService.insertReport(Reflect.on(en).field(idName).get()));
            }
            if (dateFeature == DataFeatureConstant.UPDATE) {
                list.forEach(en -> reportService.updateReport(Reflect.on(en).field(idName).get()));
            }
            if (dateFeature == DataFeatureConstant.DELETE) {
                list.forEach(en -> reportService.deleteReport(Reflect.on(en).field(idName).get()));
            }
            LOG.info("{}执行成功", jobName);
        }
    }
}
